import { createRouter, createWebHashHistory, createWebHistory } from "vue-router";
// routes
const routes = [
    {
        redirect: "/home",
        path: "/"
    },
    // 首页
    {
        path: '/home',
        name: "home",
        component: () => import("@/view/home/index.vue")
    },
    // 管理页
    {
        path: '/manage',
        name: "manage",
        component: () => import("@/view/manage/index.vue"),
        children: [

            // ---------- 机构负责人 ----------
            // 机构菜单路由
            {
                path: 'organizationinfo',
                name: "manage-organizationinfo",
                component: () => import('@/view/manage/menusubassembly/organizationinfo.vue'),
                meta: {
                    index: "manage-organizationinfo",
                }
            },
            // 人员管理路由
            {
                path: 'personnel',
                name: "manage-personnel",
                component: () => import('@/view/manage/menusubassembly/personnel.vue'),
                meta: {
                    index: "manage-personnel",
                }
            },
            // 报名管理路由
            {

                path: 'registration',
                name: "manage-registration",
                component: () => import('@/view/manage/menusubassembly/registration.vue'),
                meta: {
                    index: "manage-registration",
                }
            },
            // 报名管理
            {
                path: 'registration/details/:eventId',
                name: "manage-registration-details",
                component: () => import('@/view/manage/menusubassembly/registration/details.vue'),
                meta: {
                    index: "manage-registration",
                }
            },
            // ID码获取
            {
                path: 'IDCodeAcquisition',
                name: "manage-IDCodeAcquisition",
                component: () => import('@/view/manage/agentmenu/idcode.vue'),
                meta: {
                    index: "manage-IDCodeAcquisition",
                }
            },

            // ---------- 组委会负责人 ----------

        ]
    },
    // auth
    {
        path: '/auth',
        redirect: "/auth/login",
        component: () => import("@/view/auth/index.vue"),
        children: [
            // 登录
            {
                path: 'login',
                component: () => import("@/view/auth/login.vue")
            },
            // 注册
            {
                path: 'reg',
                component: () => import("@/view/auth/reg.vue")
            },
            // 忘记密码
            {
                path: 'forgetpassword',
                name: 'forgetpassword',
                component: () => import("@/view/auth/forgetpassword.vue")
            },
        ]
    },
    // 对于所有未定义的路径,自动匹配404页面
    {
        path: '/:pathMatch(.*)*', component: () => import("@/view/404.vue")
    },
]

// router
const router = createRouter({
    history: import.meta.env.VITE_MODE == 'dev' ? createWebHashHistory() : createWebHistory(),
    routes
});
// 无需登录的页面
const LOGIN_ROUTERS = "manage"
router.beforeEach((to, from, next) => {
    if (to.fullPath.indexOf(LOGIN_ROUTERS) != -1) {
        let token = localStorage.getItem("token")
        if (!token) {
            return next("/auth/login");
        }
    }
    return next(true)
})
export default router;